/*
* IBM Confidential
* (C) Copyright IBM Corp. 2011 All Rights Reserved.
*/
package com.ibm.crm.tma.adapter.mq.testplan;

import java.util.Timer;

/**
 * <p>
 * This class is the task responsible for cancelling the Timer used
 * for scheduling test plan operations and notifiying other threads
 * about the cancellation of the timer by using the synchronizationObject.
 * It extends BaseTimerTask to gain access to the TMA_MESSAGE_LOG, and
 * overrides run() method from java.util.TimerTask. It can be scheduled
 * using java.util.Timer
 * The Timer to cancel and the object to synchronize on are passed
 * to the constructor and set to the namesake instance fields.
 * </p>
 * <p>
 * Thread Safety:
 * This class is immutable and thus thread safe. its fields are
 * initialized in the constructor and never changed after that.
 * in addition this class is not expected to be shared by multiple
 * threads, each thread will create its own instance of it.
 * </p>
 * @author Schpotsky, TCSDEVELOPER
 * @version 1.0
 */
public class MainTimerCancellerTimerTask extends BaseTimerTask {
    /**
     * <p>
     * The Timer to cancel.
     * It is initialized in the constructor and never changed after that.
     * Can not be null.
     * It is immutable.
     * It is used in run().
     * </p>
     */
    private final Timer timer;

    /**
     * <p>
     * The Object instance to synchronize on when notifiying the thread
     * waiting for the cancellation operation of the timer.
     * It is initialized in the constructor and never changed after that.
     * Can not be null.
     * It is immutable.
     * It is used in run().
     * </p>
     */
    private final Object synchronizationObject;

    /**
     * <p>
     * Creates an instance of this class with the passed-in timer and
     * synchronizationObject.
     * </p>
     * Implementation Notes:
     * this.timer = timer;
     * this.synchronizationObject = synchronizationObject;
     * @param synchronizationObject
     * The Object instance to synchronize on when notifiying
     * the waiting thread.
     * @param timer
     *            The Timer instance.
     */
    public MainTimerCancellerTimerTask(final Timer timer,
            final Object synchronizationObject) {
        super();
        if (super.getTMAMessageLog() == null) {
            throw new IllegalArgumentException("TMA_MESSAGE_LOG "
                    + "cannot be null");
        }
        if (timer == null) {
            super.getTMAMessageLog().logError("TMA_PLAN_MANAGER_E"
                    , MainTimerCancellerTimerTask.class.getName(),
                    "MainTimerCancellerTimerTask(Timer timer,"
                    + " Object synchronizationObject)"
                    , "Parameter timer cannot be null");
            throw new IllegalArgumentException("Parameter "
                    + "timer cannot be null");
        }

        if (synchronizationObject == null) {
            super.getTMAMessageLog().logError("TMA_PLAN_MANAGER_E"
                    , MainTimerCancellerTimerTask.class.getName(),
                    "MainTimerCancellerTimerTask(Timer timer,"
                    + " Object synchronizationObject)"
                    , "Parameter synchronizationObject cannot be null");
            throw new IllegalArgumentException("Parameter "
                    + "synchronizationObject cannot be null");
        }
        this.timer = timer;
        this.synchronizationObject = synchronizationObject;
    }

    /**
     * <p>
     * Executes this task.
     * It simply cancels the timer (aggregated field timer)
     * and notifies the waiting thread by synchronizing on
     * the synchronizationObject.
     * </p>
     * Implementation Notes :
     * // logging should be performed in this method as
     * described in the section 1.3.1 of the CS.
     *
     * 1. // Cancel the timer
     *  1.1 - timer.cancel();
     * 2. // notify the waiting thread
     *   2.1 - synchronized(synchronizationObject) {
     *      2.1.1 - synchronizationObject.notify();
     *    }
     */
    public void run() {
        super.getTMAMessageLog().logInformation("TMA_PLAN_MANAGER_I"
                , MainTimerCancellerTimerTask.class.getName(), "run()"
                , "Main Timer Canceller Timer Task is running");

        timer.cancel();
        synchronized (synchronizationObject) {
            synchronizationObject.notify();
        }
    }
}

